package com.yj.suncity.publish;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.DialogUtils.ItemCall;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class PublishCityHelpActivity extends Activity implements
		OnClickListener {
	private Context context;
	private EditText mEdt_price, mEdt_content,mEdt_help_theme;
	private TextView mTxt_type;
	private String publishType;
	private List<NameValuePair> pairs;
	private String userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_publish_city_help);
		context = this;
		pairs = new ArrayList<NameValuePair>();
		userId = getIntent().getStringExtra("userid");
		findView();
	}

	private void findView() {
		mTxt_type = (TextView) findViewById(R.id.txt_type);
		mTxt_type.setOnClickListener(this);
		mEdt_price = (EditText) findViewById(R.id.edt_price);
		mEdt_content = (EditText) findViewById(R.id.edt_content);
		mEdt_help_theme=(EditText) findViewById(R.id.edt_help_theme);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.txt_type:
			DialogUtils dialogUtils = DialogUtils.getInstance();
			dialogUtils.PulishHelpDialog(context, new ItemCall() {
				@Override
				public void onIndexCall(int index) {
					if (index == 0) {
						mTxt_type.setText("悬赏");
						publishType = "1";
					} else if (index == 1) {
						mTxt_type.setText("急事");
						publishType = "2";
					} else if (index == 2) {
						mTxt_type.setText("活动");
						publishType = "3";
					}
				}
			});

			break;
		case R.id.btn_ok:
			String theme = mEdt_help_theme.getText().toString().trim();
			String type = mTxt_type.getText().toString().trim();
			String price = mEdt_price.getText().toString().trim();
			String content = mEdt_content.getText().toString().trim();
			if (theme.equals("")) {
				ToastUtil.ShowToast(context, "请输入帮帮主题！");
				return;
			}
			if (type.equals("")) {
				ToastUtil.ShowToast(context, "请选择发布类型！");
				return;
			}
			if (price.equals("")) {
				ToastUtil.ShowToast(context, "请输入悬赏！");
				return;
			}
			if (content.equals("")) {
				ToastUtil.ShowToast(context, "请输入发布内容！");
				return;
			}
			publish(theme,content, price);

			break;
		default:
			break;
		}
	}

	private void publish(String theme,String content, String price) {
		// 接口参数 action= publishcityhelp
		// 提交参数 userid(发布用户ID)、helpcontent(内容)、givenum(银元数)、helptype(1悬赏 2急事
		// 3活动)
		// 权限限制 无
		// 失败返回值 {“success”:0,”msg”:”发布失败” }
		// 成功返回值 {“success”:1,”msg”:”发布成功” }
		pairs.clear();
		String path = URLCon.同城帮帮;
		pairs.add(new BasicNameValuePair("action", "publishcityhelp"));
		pairs.add(new BasicNameValuePair("userid", userId));
		pairs.add(new BasicNameValuePair("title", theme));
		pairs.add(new BasicNameValuePair("helpcontent", content));
		pairs.add(new BasicNameValuePair("givenum", price));
		pairs.add(new BasicNameValuePair("helptype", publishType));
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, pairs, null,
				false, null, true, "正在发布......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							ToastUtil.ShowToast(context, "发布成功！");
							PublishCityHelpActivity.this.finish();
						} else {
							ToastUtil.ShowToast(context,
									""+jsonObject.optString("msg"));
						}

					}
				});
		asyncTaskUtil.execute(path);
	}
}
